package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component("fileStatisticService")
public class FileStatisticService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  //文件管理概览
  public BaseResult fileMgrOverview(long userId, List<Long> groupIds) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/filemgr/overview?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }


  //文件管理概览，30天数据容量变化趋势
  public BaseResult getTotalCapacityByDays(long userId, List<Long> groupIds, int days) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/filemgr/capacity?ownerId=%d&groupIds=%s&days=%d",
          server, port, userId, groupIdStr, days);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //数据源存储排名
  public BaseResult getSourceRankCapacity(long userId, List<Long> groupIds, int topN) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/capacityrank?ownerId=%d&groupIds=%s&topN=%d",
          server, port, userId, groupIdStr, topN);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //数据源存储饼图
  public BaseResult getSourceCapacityPie(long userId, List<Long> groupIds) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/capacitytop?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //数据集存储排名
  public BaseResult getSetRankCapacity(long userId, List<Long> groupIds, int topN) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/dataset/capacityrank?ownerId=%d&groupIds=%s&topN=%d",
          server, port, userId, groupIdStr, topN);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //数据源概览
  public BaseResult sourceOverview(String sourceId, long userId, List<Long> groupIds) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/overview?sourceId=%s&ownerId=%d&groupIds=%s",
          server, port, sourceId, userId, groupIdStr);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //单个数据源近30天文件数
  public BaseResult sourceFileNumsByDays(String sourceId, int days) {
    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/file?sourceId=%s&days=%d",
          server, port, sourceId, days);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //单个数据源近30天存储容量
  public BaseResult sourceCapacityByDays(String sourceId, int days) {
    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/capacity?sourceId=%s&days=%d",
          server, port, sourceId, days);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //单个数据集近30天存储容量
  public BaseResult dataSetCapacityByDays(String dataSetId, int days) {
    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/dataset/capacity?dataSetId=%s&days=%d",
          server, port, dataSetId, days);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //立即执行数据源统计
  public BaseResult startSourceStat(String dataSourceId) {
    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/start?sourceId=%s", server, port, dataSourceId);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .getForEntity(execUrl, BaseResult.class);
    return responseEntity.getBody();
  }

  //立即停止数据源统计
  public BaseResult stopSourceStat(String dataSourceId) {
    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/source/stop?sourceId=%s", server, port, dataSourceId);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .getForEntity(execUrl, BaseResult.class);
    return responseEntity.getBody();
  }

  public BaseResult getDataCenterCapacityPie(Long userId, List<Long> groupIds) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/datacenter/capacitytop?ownerId=%d&groupIds=%s",
          server, port, userId, groupIdStr);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //数据中心存储排名
  public BaseResult getDataCenterRankCapacity(long userId, List<Long> groupIds, int topN) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/datacenter/capacityrank?ownerId=%d&groupIds=%s&topN=%d",
          server, port, userId, groupIdStr, topN);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }

  //30天数据中心逐日增量变化趋势
  public BaseResult getRecentDataCenterDailyIncrements(long userId, List<Long> groupIds, int days) {
    String groupIdStr = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));

    String execUrl = String.format(
          "http://%s:%s/monitor/stat/file/filemgr/datacenter/dailyincrement?ownerId=%d&groupIds=%s&days=%d",
          server, port, userId, groupIdStr, days);

    return restTemplate
          .getForEntity(execUrl, BaseResult.class)
          .getBody();
  }
}
